/**
 * Created by ruiyuan on 15-8-24.
 */

var logService = require('../services/appLogService');
var messageUtil = require('../common/utils/messageUtil');
var toolUtil = require('../common/utils/toolUtil');

/*  宝宝树66小区二次改造    add   guo.xiaomin  20170802   start   */
var userService = require('../services/userService');
/*  宝宝树66小区二次改造    add   guo.xiaomin  20170802   end   */

exports.postAppRequestStatus = function (req, res) {
    var input = req.body;

    input.deviceId = req.params.deviceId;

    var params = {};
    var tempArray = input.url.split('?');
    if (tempArray.length == 2) {
        var parts = tempArray[1].split('&');
        for (var i = 0; i < parts.length; i++) {
            var nv = parts[i].split('=');
            if (!nv[0]) continue;
            params[nv[0]] = nv[1] || true;
        }
    }

    input.type = params.type;
    input.vname = params.vname;
    input.vcode = params.vcode;
    input.channelId = params.channelId;


    toolUtil.putMessageAppRequestLogMQ(input);

    res.json({
        "serviceCode": 3000,
        "result": {
            "status": 1,
            "message": "ok"
        },
        "data": null
    })
};

/**
 * 点击采集
 * @param req
 * @param res
 */
exports.acquisitionClickData = function (req, res) {

    res.json({
        "serviceCode": 3000,
        "result": {
            "status": 1,
            "message": "ok"
        },
        "data": null
    });

    var input = req.body;

    var message = {
        userId: input.userId.length > 24 ? toolUtil.getUserIdByToken(input.userId) : input.userId,
        type: input.type,
        clickSource: input.clickSource,
        typeDesc: input.typeDesc,
        ext: input.ext,
        deviceId: req.params.deviceId,
        vname: req.query.vname,
        vcode: req.query.vcode,
        os: req.query.type,
        createTime: new Date().getTime()
    };
    toolUtil.putMessageLogMQ(message);
};

/*  宝宝树66小区二次改造    mod   guo.xiaomin  20170802   start   */
exports.postUserLbs = function (req, res) {

    var token = req.params.token;

    userService.getUserGagOrNotService(token, function (err, results) {
        if (err) {
            logger.error("Failed to post question - " + err.message);
            res.json(messageUtil.composeFailedMessage(req, "Failed to post question - " + err.message));
        } else {
            logger.debug("results._doc.gagOrNot:::" +  results._doc.gagOrNot);
            var gagOrNot = "0";
            if(results._doc.gagOrNot) {
                gagOrNot = "1";
            }
            var input = req.body;
            input.user = token;
            input.deviceId = req.params.deviceId;
            input.vName = req.query.vname;
            input.vCode = req.query.vcode;
            input.os = req.query.type;

            logService.postUserLbsService(input, function () {
                res.json({
                    "serviceCode": 3000,
                    "result": {
                        "status": 1,
                        "message": "ok"
                    },
                    "data": {"gagOrNot": gagOrNot}
                });
            })
        }
    });
};
/*  宝宝树66小区二次改造    mod   guo.xiaomin  20170802   end   */
